#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <memory>

// 简化版单聊演示 - 无交互，直接展示功能

struct SimpleMessage {
    std::string fromUser;
    std::string toUser;
    std::string content;
    time_t timestamp;
    
    SimpleMessage(const std::string& from, const std::string& to, const std::string& msg)
        : fromUser(from), toUser(to), content(msg), timestamp(time(nullptr)) {}
};

struct ChatSession {
    std::string chatId;
    std::string user1;
    std::string user2;
    std::vector<SimpleMessage> messages;
    
    ChatSession(const std::string& u1, const std::string& u2) : user1(u1), user2(u2) {
        if (u1 < u2) {
            chatId = u1 + "_" + u2;
        } else {
            chatId = u2 + "_" + u1;
        }
    }
    
    void addMessage(const SimpleMessage& msg) {
        messages.push_back(msg);
    }
};

class QuickChatDemo {
private:
    std::unordered_map<std::string, std::shared_ptr<ChatSession>> chats;
    
    std::string generateChatId(const std::string& user1, const std::string& user2) {
        if (user1 < user2) {
            return user1 + "_" + user2;
        } else {
            return user2 + "_" + user1;
        }
    }
    
public:
    void startChat(const std::string& user1, const std::string& user2) {
        std::string chatId = generateChatId(user1, user2);
        if (chats.find(chatId) == chats.end()) {
            chats[chatId] = std::make_shared<ChatSession>(user1, user2);
            std::cout << "💬 创建新会话: " << chatId << std::endl;
        }
    }
    
    void sendMessage(const std::string& from, const std::string& to, const std::string& content) {
        std::string chatId = generateChatId(from, to);
        
        // 自动创建会话
        if (chats.find(chatId) == chats.end()) {
            startChat(from, to);
        }
        
        SimpleMessage msg(from, to, content);
        chats[chatId]->addMessage(msg);
        
        std::cout << "📤 [" << from << " -> " << to << "] " << content << std::endl;
    }
    
    void showChatHistory(const std::string& user1, const std::string& user2) {
        std::string chatId = generateChatId(user1, user2);
        auto it = chats.find(chatId);
        
        if (it == chats.end()) {
            std::cout << "📭 " << user1 << " 和 " << user2 << " 没有聊天记录" << std::endl;
            return;
        }
        
        auto chat = it->second;
        std::cout << "\n📋 " << user1 << " 和 " << user2 << " 的聊天记录:" << std::endl;
        std::cout << "🆔 会话ID: " << chat->chatId << std::endl;
        std::cout << "📊 消息总数: " << chat->messages.size() << " 条" << std::endl;
        
        for (size_t i = 0; i < chat->messages.size(); ++i) {
            const auto& msg = chat->messages[i];
            std::cout << "  " << (i + 1) << ". [" << msg.fromUser << "] " << msg.content << std::endl;
        }
    }
    
    void showAllChats() {
        std::cout << "\n📊 所有会话总览:" << std::endl;
        for (const auto& pair : chats) {
            auto chat = pair.second;
            std::cout << "🔹 " << chat->chatId << " (" << chat->messages.size() << " 条消息)" << std::endl;
        }
    }
    
    void runDemo() {
        std::cout << "🚀 单聊系统快速演示" << std::endl;
        std::cout << std::string(50, '=') << std::endl;
        
        std::cout << "\n📱 第一步：开启单聊" << std::endl;
        std::cout << "Alice 想和 Bob 聊天..." << std::endl;
        sendMessage("Alice", "Bob", "Hi Bob! 你好吗？");
        
        std::cout << "\n📱 第二步：Bob 回复" << std::endl;
        sendMessage("Bob", "Alice", "我很好，谢谢！你呢？");
        
        std::cout << "\n📱 第三步：继续对话" << std::endl;
        sendMessage("Alice", "Bob", "我也很好！今天天气不错呢");
        sendMessage("Bob", "Alice", "是的，要不要一起出去走走？");
        
        std::cout << "\n📱 第四步：Alice 和 Charlie 开始新对话" << std::endl;
        sendMessage("Alice", "Charlie", "嗨 Charlie！");
        sendMessage("Charlie", "Alice", "Hello Alice! 最近怎么样？");
        
        std::cout << "\n📋 查看聊天记录:" << std::endl;
        showChatHistory("Alice", "Bob");
        showChatHistory("Alice", "Charlie");
        
        std::cout << "\n📊 系统统计:" << std::endl;
        showAllChats();
        
        std::cout << "\n✨ 演示完成！" << std::endl;
        std::cout << "\n🎯 关键特性展示:" << std::endl;
        std::cout << "  ✅ 自动会话创建：发消息时自动建立会话" << std::endl;
        std::cout << "  ✅ 唯一会话ID：Alice_Bob 和 Bob_Alice 是同一个会话" << std::endl;
        std::cout << "  ✅ 消息历史：所有消息自动保存" << std::endl;
        std::cout << "  ✅ 多重对话：一个用户可以同时和多人聊天" << std::endl;
        std::cout << "  ✅ 点对点通信：消息只在两个用户间传递" << std::endl;
        
        std::cout << "\n🔍 与传统群聊对比:" << std::endl;
        std::cout << "  📡 消息路由：点对点 vs 一对多广播" << std::endl;
        std::cout << "  🔒 隐私保护：完全私密 vs 群内可见" << std::endl;
        std::cout << "  ⚡ 性能开销：O(1) vs O(n)" << std::endl;
        std::cout << "  💾 存储方式：按对话 vs 按群组" << std::endl;
    }
};

int main() {
    QuickChatDemo demo;
    demo.runDemo();
    return 0;
} 